package com.zznj.xzjc.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxWebViewTool;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.index.AjcxXzcfAdapter;
import com.zznj.xzjc.adapter.index.HdxxHfxxAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.AjcxData;
import com.zznj.xzjc.bean.index.HdlxData;
import com.zznj.xzjc.bean.index.HdlxDetailResult;
import com.zznj.xzjc.bean.index.HdlxHfxxData;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.XsglxqResult;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityHdlxDetail extends BaseActivity {


    Unbinder unbinder;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.dwsj)
    TextView dwsj;
    @BindView(R.id.time)
    TextView time;

    @BindView(R.id.webView)
    WebView webView;


    @BindView(R.id.replyLinear)
    View replyLinear;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rec_main)
    RecyclerView rec_main;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_hdlx_detail_layout;
    }

    private String objId;
    private String msgId;
    private int type; // 0--发送；1--接收

    @Override
    public void initView() {
        objId = getIntent().getExtras().getString("objId");
        msgId = getIntent().getExtras().getString("msgId");
        setTitle("交流详情");
        initSrl();
        init();
    }

    @Override
    protected void initData() {
        getData();
        if(!StringUtils.isEmpty(msgId)){
            readMsg();
        }
    }


    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(false);
        srlAll.setEnableAutoLoadMore(false);
        srlAll.setEnableOverScrollDrag(false);
        srlAll.setOnRefreshListener(refresh_layout -> {
            getData();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
        });
    }

    private List<HdlxHfxxData> list;
    private HdxxHfxxAdapter mAdapter;

    private void init() {
        list = new ArrayList<>();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new HdxxHfxxAdapter(R.layout.gyss_item_hdlx_hfxx, list);
        rec_main.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    private void getData() {
        SubscriberOnNextListener<BaseBean<HdlxDetailResult>> onNextListener = baseBean -> {
            if(srlAll != null){
                srlAll.finishRefresh();
            }
            title.setText(baseBean.getResult().getCommunionSend().getTitle());
            dwsj.setText("发起部门：" + baseBean.getResult().getCommunionSend().getSendOrgName());
            time.setText("发送时间：" + TimeUtils.getTime(baseBean.getResult().getCommunionSend().getSendTime(), "yyyy-MM-dd"));
            RxWebViewTool.loadData(webView, baseBean.getResult().getCommunionSend().getContent());
            if(baseBean.getResult().getReplyList() == null || baseBean.getResult().getReplyList().size() == 0){
                replyLinear.setVisibility(View.GONE);
            }else{
                replyLinear.setVisibility(View.VISIBLE);
                mAdapter.setNewData(baseBean.getResult().getReplyList());
            }
        };
        HttpMethods.getInstance().hdlxSendDetail(new ProgressSubscriber<>(onNextListener, mContext, true), objId);
    }

    private void readMsg() {
        SubscriberOnNextListener<BaseBean<XsglxqResult>> onNextListener = baseBean -> {

        };
        FormBody.Builder builder = new FormBody.Builder()
                .add("msgId", msgId);
        HttpMethods.getInstance().updStatus(new ProgressSubscriber<>(onNextListener, mContext, true), builder.build());
    }
}
